
<%@page import="org.ow2.bonita.facade.uuid.ActivityInstanceUUID"%><%@ page import="javax.security.auth.login.LoginContext"%>

<%@ page import="org.ow2.bonita.facade.QueryRuntimeAPI"%>
<%@ page import="org.ow2.bonita.facade.uuid.ProcessInstanceUUID"%>
<%@ page import="org.ow2.bonita.util.AccessorUtil"%>
<%@ page import="org.ow2.bonita.util.SimpleCallbackHandler"%>
<%@page import="org.ow2.bonita.facade.runtime.TaskInstance"%>
<%@page import="java.util.Collections"%>
<%@page import="java.util.Map"%>
<%@page import="java.text.*"%>
<%@page import="org.ow2.bonita.facade.def.majorElement.ActivityDefinition"%>
<%@page import="org.ow2.bonita.facade.QueryDefinitionAPI"%>
<%@page import="java.util.Set"%>
<%@page import="org.ow2.bonita.facade.def.majorElement.DataFieldDefinition"%>
<%@page import="java.util.HashMap"%>
<%@page import="org.ow2.bonita.facade.runtime.ActivityState"%>
<%@ page import="org.ow2.bonita.facade.QueryDefinitionAPI"%>
<%@ page import="org.ow2.bonita.util.AccessorUtil" %>
<%@ page import="org.ow2.bonita.light.LightProcessDefinition" %>
<%@ page import="javax.security.auth.login.LoginContext" %>
<%@ page import="org.ow2.bonita.util.SimpleCallbackHandler" %>
<%@ page import="org.ow2.bonita.facade.ManagementAPI" %>
<%@ page import="org.ow2.bonita.facade.QueryRuntimeAPI"%>
<%@ page import="org.ow2.bonita.facade.RuntimeAPI"%>
<%@ page import="org.ow2.bonita.facade.runtime.ActivityInstance" %>
<%@ page import="org.ow2.bonita.facade.def.element.BusinessArchive"%>
<%@ page import="org.ow2.bonita.facade.def.majorElement.ProcessDefinition"%>
<%@ page import="org.ow2.bonita.facade.runtime.ActivityState"%>
<%@ page import="org.ow2.bonita.facade.runtime.InstanceState"%>
<%@ page import="org.ow2.bonita.facade.runtime.TaskInstance"%>
<%@ page import="org.ow2.bonita.facade.uuid.ProcessDefinitionUUID"%>
<%@ page import="org.ow2.bonita.util.BonitaConstants"%>
<%@ page import="org.ow2.bonita.util.BusinessArchiveFactory"%>
<%@ page import="org.ow2.bonita.facade.runtime.ProcessInstance" %>
<%@ page import="java.util.Collection"%>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.Set" %>
<%@ page import="java.util.List" %>
<%@ page import="org.ow2.bonita.facade.runtime.ActivityState" %>
<%@ page import="org.ow2.bonita.facade.def.majorElement.ActivityDefinition" %>
<%@ page import="java.io.File" %>
<%@ page import="java.nio.file.Paths" %>
<%@ page import="org.ow2.bonita.util.BusinessArchiveFactory" %>
<%@ page import="org.ow2.bonita.facade.BAMAPI" %>
<%@ page import="org.bonitasoft.console.client.reporting.TimeUnit" %>
<%@ page import="org.ow2.bonita.facade.def.majorElement.ActivityDefinition.Type"%>
<%@ page import="org.ow2.bonita.facade.uuid.ActivityDefinitionUUID" %>
<%@ page import="org.ow2.bonita.facade.uuid.ProcessDefinitionUUID" %>
<%@ page import="org.bonitasoft.console.server.bam.birt.datasource.*"%>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.Calendar" %>
<%@ page import="java.util.*" %>
<%@page import="utility.Conversione" %>

<%
final Object isAdminObject = request.getSession().getAttribute("isadmin");
final boolean isAdmin = isAdminObject != null && ((Boolean) isAdminObject).booleanValue();

final String puuid = request.getParameter("puuid");
final String msg = request.getParameter("msg");
TaskInstance task = null;
ProcessInstanceUUID instanceUUID = null;
Map<String, String> localDatafieldsTypes = new HashMap<String, String>();
Map<String, String> globalDatafieldsTypes = new HashMap<String, String>();
Map<String, Object> localVariables = Collections.emptyMap();
Map<String, Object> globalVariables = Collections.emptyMap();
String candidates = "";
boolean isReady = false;

try {
  final String username = (String) request.getSession().getAttribute("username");
  final LoginContext loginContext = new LoginContext("BonitaStore", new SimpleCallbackHandler(username, ""));
  loginContext.login();
  
} catch (Exception e) {
  e.printStackTrace();
  Throwable t = e;
  while (t.getCause() != null) {
    t = t.getCause();
  }
  final String errorMsg = "Error while getting KPI";
  response.sendRedirect("home.jsp?msg=" + errorMsg);  
}

QueryDefinitionAPI queryDefinitionAPI = AccessorUtil.getQueryDefinitionAPI();
final ManagementAPI managementAPI = AccessorUtil.getManagementAPI();
final QueryRuntimeAPI queryRuntimeAPI = AccessorUtil.getQueryRuntimeAPI();

BAMAPI bam=AccessorUtil.getBAMAPI();

 Set<ProcessDefinition> processesDefinitions = queryDefinitionAPI.getProcesses();
Iterator<ProcessDefinition> processesDefinitionsIterator = processesDefinitions.iterator();
ProcessDefinitionUUID idp=null;
ProcessDefinition defP=null;
while(processesDefinitionsIterator.hasNext()) {
	
	ProcessDefinition processDefinition = processesDefinitionsIterator.next(); 
	ProcessDefinitionUUID Puuid= processDefinition.getUUID();
	if (Puuid.toString().equals(puuid)){
		idp=Puuid;
		defP=processDefinition;
	}
}
Date data1, data2;
data1=defP.getDeployedDate();
Calendar c = Calendar.getInstance();
data2=c.getTime();//data corrente 
String nameP=defP.getName();

System.out.println(data1 + "\n" + data2);
%>


<jsp:include page="header.html"/>
<a href="home.jsp">Home</a>&nbsp;&nbsp;&nbsp;
<%if (isAdmin) {%>
  <a href="processes.jsp">Manage processes</a>&nbsp;&nbsp;&nbsp;
  <a href="instances.jsp">Manage instances</a>&nbsp;&nbsp;&nbsp;
  <a href="KPI.jsp">KPI Home </a>&nbsp;&nbsp;&nbsp;
<%}%>
<br><br>

<%if (msg != null) {%>
  <b><%=msg %></b><br><br>
<%
	}

String user=(String) request.getSession().getAttribute("username");
List<Long> wait = bam.getTaskInstancesWaitingTimeOfUser(user, idp, data1, data2);
String conc_d= new String(); 
String conc_d1,conc_d2;
Conversione con=new Conversione();

Iterator<Long> it2 = wait.iterator();
System.out.println("Size = "+ wait.size());
while (it2.hasNext()){
	System.out.println("it="+it2.next());
}

Iterator<Long> it=wait.iterator();
while (it.hasNext()){
	
	 conc_d=conc_d+"<p>"+con.getTime(it.next())+"</p>";
	}

wait = bam.getActivityInstancesDuration(idp, data1, data2);

conc_d1=new String();
it=wait.iterator();
System.out.println("size it activity duration="+wait.size());
while (it.hasNext()){
	  conc_d1=conc_d1+"<p>"+con.getTime(it.next())+"</p>";
	 
	}

wait = bam.getActivityInstancesExecutionTime(idp, data1, data2);
conc_d2=new String();

it=wait.iterator();
while (it.hasNext()){

	  conc_d2=conc_d2+"<p>"+con.getTime(it.next())+"</p>";
	 
}

//wait = bam.
%>
<h1> KPI Base for <%=nameP%> </h1> <br> </br>
<TABLE BORDER="1" CELLPADDING="5" WIDTH="600px">
<CAPTION> <b> Deployed Date: </b> <%=data1%> </CAPTION> 
<CAPTION> <b> Current Date:</b> <%=data2%></CAPTION>  
<br></br>
<TR>
  	<%out.println("<td>N of created activity instances</td> <td>"+bam.getNumberOfCreatedActivityInstances(idp, data1, data2)+"</td>"); %>
</TR>
<TR>
 	<%out.println("<td>N of created process instances</td> <td>"+bam.getNumberOfCreatedProcessInstances(idp, data1, data2)+"</td>"); %>
</TR>
<TR>

 	<% if(!conc_d1.equals(""))
 	   		out.println("<td>Duration of all finished activity instances </td> <td>"+conc_d1+"</td>"); 
 		else 
 			out.println("<td>Duration of all finished activity instances </td> <td> - </td>");
 	   %>
</TR>
<TR>
 	<%
 	if(!conc_d.equals(""))
 	 	out.println("<td>Task instances waiting Time of user "+ user+ "</td> <td>"+conc_d+"</td>");
 	else
 		out.println("<td>Task instances waiting Time of user "+ user+ "</td> <td> - </td>");
	%>
 </TR>
 <TR>
 	<%  if(!conc_d2.equals(""))
 	    	out.println("<td>Execution time of instances activities </td> <td>"+conc_d2+"</td>"); 
 	    else
 			out.println("<td>Execution time of instances activities </td> <td> - </td>");	
    %>
</TR>
</TABLE>

<br><br>
<jsp:include page="footer.html"/>